<?php
include(PATH_GI_VENDOR . "phpMailer/class.phpmailer.php");
//error_reporting(E_ALL);
class Helper_Mailer{
	
	protected  $mail;
	
	
	function  __construct(){
		$mail             = new PHPMailer();
	
		//$body             = $mail->getFile('contents.html');
		//$body             = eregi_replace("[\]",'',$body);
	
		$mail->IsSMTP();
		$mail->SMTPAuth   = true;                  // enable SMTP authentication
		$mail->SMTPSecure = "tls";                 // sets the prefix to the servier
		$mail->Host       = "smtp1.szs.st.com";      // sets GMAIL as the SMTP server
		$mail->Port       = 25;                   // set the SMTP port for the GMAIL server
	
		$mail->Username   = "ictestpm";  // MAIL username
		$mail->Password   = "ictest123456";            // MAIL password
		$mail->CharSet	  = "utf-8"; // ����ָ���ַ�

		$mail->AddReplyTo("ictestpm@st.com","ICTESTPM");
	
		$mail->From       = "ictestpm@st.com";
		$mail->FromName   = "ICTESTPM";
		
		$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
		
		$mail->WordWrap   = 50; // set word wrap
	
	
		$mail->IsHTML(true); // send as HTML
		
		$this->mail = $mail;
		
		return $this->mail;
	}
	
	
	function setContent($subject,$body,$attachments=array()){
		$mail = $this->mail;
		
		$mail->Subject    = $subject;
		$mail->body 	  = $body;
		
		$mail->MsgHTML($body);
		
		foreach ($attachments as $attachment){
			$mail->AddAttachment($attachment);             // attachment
		}
	}
	
	function send($to=array(), $cc=array(), $bcc=array()){
		$mail = $this->mail;
		
		foreach ($to as $address){
			$address = trim($address);
			if($this->isValidAddress($address)){
				$mail->AddAddress($address);
			}
		}
		foreach ($cc as $address){
			$address = trim($address);
			if($this->isValidAddress($address)){
				$mail->AddCC($address);
			}
		}
		
		foreach ($bcc as $address){
			$address = trim($address);
			if($this->isValidAddress($address)){
				$mail->AddBCC($address);
			}
		}
		
		if(!$mail->Send()) {
		  	return false;
			//echo "Mailer Error: " . $mail->ErrorInfo;
		} else {
			return true;
		  //echo "Message sent!";
		}
	}
	
	function isValidAddress($email){
		return eregi("^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3}$",$email);
	}
	
}